package cookie

import (
	"net/http"
	"thais/utils"
	"thais/utils/jwt"
	"time"
)

const (
	COOKIE_NAME      = `THAIS_COOKIE`
	COOKIE_NAME_TTL  = 1e9 * 60 * 60 * 24
	COOKIE_NAME_API  = `THAIS_API_COOKIE`
	BASE64_STR       = `THAIS_STR_CODER` + `:` + COOKIE_NAME + `:`
	COOKIE_AUTH_USER = `COOKIE_AUTH_USER`
	COOKIE_AUTH_KEY  = `COOKIE_AUTH_KEY`
)

// new thais cookie
func NewCookie(exp ...string) *http.Cookie {
	et := int(time.Hour * 24 / time.Second)
	if len(exp) > 0 {
		switch exp[0] {
		case "expire":
			et = -1
		}
	}
	cookieValue := utils.EncodeStr(BASE64_STR)
	cookieValue, _ = jwt.GetJWT3(cookieValue)
	return &http.Cookie{
		Name:     COOKIE_NAME,
		Value:    cookieValue,
		Path:     "/",
		Secure:   false,
		HttpOnly: true,
		MaxAge:   et,
	}
}

// new auth cookie
func NewApiCookie(data interface{}, exp ...string) *http.Cookie {
	et := int(time.Hour * 24 / time.Second)
	if len(exp) > 0 {
		switch exp[0] {
		case "expire":
			et = -1
		}
	}
	cookieValue, _ := jwt.GetJWT3(data)
	return &http.Cookie{
		Name:     COOKIE_NAME_API,
		Value:    cookieValue,
		Path:     "/",
		Secure:   false,
		HttpOnly: true,
		MaxAge:   et,
	}
}
